function launchAppEnhance(app_name) {
    let thread = threads.start(function () {
        toastLog("检查是否有打开权限!");
        while (true) {
            var allow = text("允许").findOne(5000);
            if (allow) {
                sleep(1000)
                toastLog("发现允许授权!");
                allow.click();
                sleep(1000);
                thread.interrupt();
                break;
            }
        }
    });
    sleep(1000);
    var result = launchApp(app_name);
    sleep(3000);
    thread.interrupt();
    return result;
}

function click_close() {
    let close = text("点击关闭").findOne(5000);
    if (close) {
        sleep(1000);
        close.click();
    }
}

// 点击控件坐标，控件的clickbale为false的时候
function 点击控件(x) {
    var weight = x.bounds()
    click(weight.centerX(), weight.centerY())
}

function view_ad_vedio() {
    let $_times = 9;
    while (true) {
        if ($_times <= 0) { break; }
        var 拒绝 = text("拒绝").findOne(1000)
        if (拒绝) {
            拒绝.click()
        }
        var close = id("reward_ad_close").findOne(1000) || id("tt_video_ad_close_layout").findOne(1000);
        if (close) {
            toastLog("发现关闭按钮");
            sleep(1000);
            close.click();
            break;
        } else {
            try {
                取消 = text("取消").findOne(500)
                if (取消) {
                    点击控件(取消)
                }
            } catch (error) { }
            try {
                关闭 = text("关闭").findOne(500)
                if (关闭) {
                    点击控件(关闭)
                }
            } catch (error) { }
            try {//支付宝
                if (currentPackage() != 'com.liziyuedong.plutuscat') {
                    sleep(2000)
                    back()
                    sleep(2000)
                    继续 = textStartsWith("继续").findOne(1000)
                    if (继续) {
                        点击控件(继续)
                    }
                    var 首页 = text('首页').findOne(5000)
                    if(首页){
                        break
                    }
                }
            } catch (error) { }
        }
        $_times--;
        sleep(3000);
    }
}

function yuanbao_dubble() {
    let fanbei = text("元宝翻倍").findOne(2000);
    if (fanbei) {
        toastLog("元宝翻倍");
        sleep(1000);
        fanbei.click();
        view_ad_vedio();
        sleep(1000);
    }
}

function jinbi_dubble() {
    let fanbei = text("金币翻倍").findOne(2000);
    if (fanbei) {
        toastLog("金币翻倍");
        sleep(1000);
        fanbei.click();
        view_ad_vedio();
        sleep(1000);
    }
}

function main() {
    launchAppEnhance("招财储蓄猫");
    var thread_1 = threads.start(function () {
        toastLog("检查广告弹窗")
        while (true) {
            var widget = id("tt_insert_express_dislike_icon_img").findOne(2000);
            if (widget) {
                widget.click();
            }
        }

    });

    for (var i = 0; i < 3; i++) {
        sleep(5000)
        //进入任务页面
        try {
            let renwu = text("任务").id("tv_main_tag_2").findOne(1000);
            if (renwu) {
                toastLog("进入任务页面!");
                renwu.parent().click();
                sleep(3000)
                jinbi_dubble();
                click_close();
                //领三餐
                let food = id("tv_task_three").findOne(2000);
                if (food) {
                    sleep(1000)
                    food.click()
                    var 系统提示 = text('系统提示').findOne(5000)
                    if (系统提示) {
                        click_close();
                    } else {
                        view_ad_vedio();
                        click_close();
                    }
                    sleep(1000);
                    break
                }
            }
        } catch (error) {
            break
        }
    }

    var shouye = text('首页').findOne(1000);
    if (shouye) {
        shouye.parent().click();
        sleep(3000);
    }

    //收取元宝
    let shouqu = id("iv_item_pig_status").findOne(5000);
    if (shouqu) {
        log("收取元宝");
        shouqu.click();
        yuanbao_dubble();
        click_close();

    }

    //领取财神
    let caishen = id("tv_main_coupon_achieve").text("可领取").findOne(2000);
    if (caishen) {
        log("领取财神");
        caishen.parent().click();
        sleep(2000)
        click_close();
    }


    //使用财神
    let use_caishen = id("tv_main_use_coupon").text("使用").findOne(2000);
    let coupon_count_obj = id("tv_main_coupon_count").findOne(2000);
    log("财神数量：" + coupon_count_obj.text());
    if (use_caishen && coupon_count_obj.text() != "x0") {
        log("使用财神");
        use_caishen.click();
        sleep(random(800, 1200));
        let liji_use = id("tv_pop_use_coupon_use").text("立即使用").findOne(2000);
        if (liji_use) {
            liji_use.click();
            sleep(1000);
        }
    }

    //领取红包
    let hogbao = id("tv_main_red_pocket_achieve").text("可领取").findOne(2000);
    if (hogbao) {
        log("领取红包");
        hogbao.parent().click();
        click_close();
    }

    //打工
    let qudagong = id("ll_game_working").findOne(2000);
    if (qudagong) {
        log("去打工");
        qudagong.click();
        sleep(1000);
        if (device.model == "Redmi 8A") {
            swipe(random(400, 500), 1300, random(400, 500), random(1200, 1300), 850);
        }
        else {
            swipe(random(400, 500), random(1500, 1600), random(400, 500), random(1200, 1300), random(700, 1500));
        }
        sleep(1000);
        let lingjiangli = text("领奖励").findOne(2000);
        if (lingjiangli) {
            lingjiangli.click();
            sleep(1000);
            let fanbei = text("红包翻倍").findOne(2000);
            if (fanbei) {
                log("红包翻倍");
                fanbei.click();
                view_ad_vedio();
                sleep(1000);
            }
            click_close();
        }

        let dagong = id("tv_working_do_5").text("去打工").findOne(1000);
        if (dagong) {
            log("点击去打工");
            dagong.click();
            sleep(1000);
        }

        dagong = id("tv_working_do_4").text("去打工").findOne(1000);
        if (dagong) {
            log("点击去打工");
            dagong.click();
            sleep(1000);
        }
    }

    //视频红包
    var shouye = text('首页').findOne(1000);
    if (shouye) {
        shouye.parent().click();
        sleep(3000);
        let count = random(15, 25);
        log("领" + count + "次红包!");
        for (i = 0; i < count; i++) {
            let video_red_pocket = text("视频红包").findOne(2000);
            if (video_red_pocket) {
                log("点击视频红包");
                sleep(1000);
                video_red_pocket.parent().click();
                view_ad_vedio();
                sleep(1000);
                最大数提示 = textStartsWith("当日操作已达最大数").findOne(2000)
                if (最大数提示) {
                    toastLog(最大数提示.text())
                    break
                }
                click_close();
            }
        }
        sleep(1000);
    }

    // var date = new Date();
    // var h = date.getHours();
    // if (h >= 20) {
    //   var shouye = text('首页').findOne(1000);
    //   if (shouye) {
    //     shouye.parent().click();
    //     sleep(3000);
    //   }
    //   let duihuan = id("tv_main_exchange").findOne(2000);
    //   if (duihuan) {
    //     log("兑换");
    //     duihuan.click();
    //     sleep(3000);

    //     var shoplevel = null
    //     var shopbutton = null
    //     var 我的金币 = id("tv_exchange_center_attend_not_gold").findOne(3000)
    //     if (我的金币) {
    //       var intmoney = parseInt(我的金币.text())
    //       if (intmoney > 10000000) {
    //         shoplevel = "高级兑换场"
    //       }
    //       else if (intmoney > 1000000) {
    //         shoplevel = "中级兑换场"
    //       }
    //       else if (intmoney > 100000) {
    //         shoplevel = "初级兑换场"
    //       }
    //       else {
    //         toastLog("金币不足参加活动" + intmoney)
    //       }

    //       shopbutton = find_button_by_textStartsWith(shoplevel)
    //       if (shopbutton) {
    //         click_button(shopbutton.bounds())
    //         let 报名 = find_button_by_textStartsWith("报名" + shoplevel)
    //         if (报名) {
    //           click_button(报名.bounds())
    //         }
    //         else {
    //           toastLog("没找到报名按钮 ")
    //         }
    //         let 确认报名 = find_button_by_textStartsWith("确认报名")
    //         if (确认报名) {
    //           click_button(确认报名.bounds())
    //         }

    //         if (textContains("已经抢过了").findOne(5000)) {
    //           click_close();
    //         }
    //       }
    //       else {
    //         toastLog("可能已经报过名了,等待兑换")
    //       }
    //     }
    //     //如果 在 com.liziyuedong.goldpig.ui.activity.ExchangeActivity 说明已经报名   
    //     // 20 点抢红包      
    //     var 现金红包 = textEndsWith("现金红包").findOne(3000)
    //     if (现金红包) {
    //       let 按钮 = 现金红包.parent().child(2)
    //       if (按钮) {
    //         按钮.click()
    //         sleep(1000);
    //         click_close();
    //       }
    //     }
    //   }
    // }
    thread_1.interrupt();
}

try {
    main();
} catch (error) {
    toastLog(error);
}

events.on("exit", function () {
    var storage = storages.create("xiangxiangyuedu");
    storage.put("script_running_status", "end");
});
